% getSession;
% sess.connect_device;
% zi=sess.getDevice('dev3567');
% client=EquipmentControl.Comm.Client('192.168.1.253', 12345);
% client.start
%%
vStart = -0.045; vEnd = 0.035; dv = 0.001;
vOut = vStart:dv:vEnd;

nPt = 10;
freq = zeros(nPt, length(vOut));
val0 = zeros(nPt, length(vOut));
val1 = zeros(nPt, length(vOut));

for k = 1:length(vOut)
    zi.setAuxOutOffset('0', vOut(k));
    pause(0.1);
    for j = 1:nPt
        [freq(j, k), val0(j, k), val1(j, k)] = getPoint(client, zi);
        pause(0.05);
    end
end

%%
figure;

centerFreq =  mean(mean(freq));
subplot(3, 1, 1)
plot(vOut, mean(freq, 1)-centerFreq, 'bo-');
title(sprintf('Center Freq = %9.6f THz', centerFreq/1e6));grid on;
xlabel('Control Voltage (V)'); ylabel('Wave Length Output (MHz)');
xlim(minmax(vOut));

subplot(3, 1, 2)
plot(vOut, mean(val0, 1), 'o-');grid on;
ylabel('Pol Spectrum (V)');
yyaxis right;
plot(vOut, mean(val1, 1), 'd-');
xlabel('Control Voltage (V)'); ylabel('Saturation Spectrum (V)');grid on;
xlim(minmax(vOut));

subplot(3, 1, 3)
plot(mean(freq, 1)-centerFreq, mean(val0, 1), 'o-');grid on;
ylabel('Pol Spectrum (V)');
yyaxis right;
plot(mean(freq, 1)-centerFreq, mean(val1, 1), 'd-');
title(sprintf('Center Freq = %9.6f THz', centerFreq/1e6));
xlabel('Wave Length Output (MHz)'); ylabel('Pol. Spectrum (V)');grid on;
xlim(minmax(mean(freq, 1)-centerFreq)); 
%%
function [freq, val0, val1] = getPoint(client, zi)
    freq = client.GetFrequency();
    val0 = zi.getAuxInValue('0');
    val1 = zi.getAuxInValue('1');
end

